function setcookie(key, val, day, path = "/") {
    if (day != undefined) {//有参数=>设置过期时间
        var date = new Date();
        date.setDate(date.getDate() + day)
        // console.log(date.toString());
        document.cookie = key + "=" + val + ";expires=" + date.toUTCString() + ";path=" + path;
    } else {//没有参数day=>默认浏览器关闭过期
        document.cookie = key + "=" + val + ";path=" + path;
    }
}


// function getcookie(key) {
//     var obj = {};
//     if (document.cookie) {
//         var list = document.cookie.split("; ")
//         // console.log(list);
//         for (var i of list) {
//             var item = i.split("=");
//             // console.log(item);
//             var [attr, val] = item
//             obj[attr] = val
//         }
//     }
//     // 全部解析之后再返回
//     return obj[key] || "";

// }


function getcookie(key) {
    var obj = {};
    if (document.cookie) {
        var list = document.cookie.split("; ")
        // console.log(list);
        for (var i of list) {
            var item = i.split("=");
            var [attr, val] = item;
            // 每次解析之后就判断
            if (key === attr) {
                return val;
            }
        }
    }
    return "";
}

function deletecookie(key) {
    setcookie(key, "", -1)
}


function clreacookie() {
    var obj = {};
    if (document.cookie) {
        var list = document.cookie.split("; ")
        // console.log(list);
        for (var i of list) {
            var item = i.split("=");
            var [attr, val] = item;
            deletecookie(attr)
        }
    }
}
